package datalookin.adapter2;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.whu.zj.MyApplication;
import com.whu.zj.R;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import datalookin.dto2.WaterPlantDataDto;
import main.dto.WaterPlant;

/**
 * Created by Administrator on 2016/6/5.
 */
public class RealPlantAdapter2 extends BaseAdapter {

    List<KV> kvs = new ArrayList<KV>();
    List<KV2> kv2s = new ArrayList<KV2>();

    LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
    List<String> keys = new ArrayList<String>();

    public void setWaterPlantDataDtos(LinkedHashMap waterPlantDataDtos) {
        this.map = waterPlantDataDtos;
        for (String str : map.keySet()) {
            if(map.get(str) != null)
                keys.add(str);
        }

        MyApplication.someType = keys;
//        initkvs();
//        initkv2s();
    }

    private void initkv2s() {
        for (int i = 0; i < kvs.size(); i++) {
            if (i % 2 == 0) {
                KV2 kv2 = new KV2();
                kv2.setKv1(kvs.get(i));
                if (i + 1 <= kvs.size() - 1)
                    kv2.setKv2(kvs.get(i + 1));
                kv2s.add(kv2);
            }
        }
    }



    String zhuangtaiHY(String status) {
        if (status.equals("-1")) {
            return "关";
        } else {
            return "开";
        }
    }


    @Override
    public int getCount() {

        int remain = keys.size() % 2;
        int lines = remain == 0 ? keys.size() / 2 : keys.size() / 2 + 1;
        return lines;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = View.inflate(MyApplication.share(), R.layout.item_realdata, null);
            viewHolder = new ViewHolder();
            viewHolder.name1 = (TextView) convertView.findViewById(R.id.tv_name1);
            viewHolder.value1 = (TextView) convertView.findViewById(R.id.tv_value1);
            viewHolder.name2 = (TextView) convertView.findViewById(R.id.tv_name2);
            viewHolder.value2 = (TextView) convertView.findViewById(R.id.tv_value2);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
//        KV2 kv2 = kv2s.get(i);
        String k1 = keys.get(i * 2);
        String v1 = map.get(k1);

        viewHolder.name1.setText(k1);
        viewHolder.value1.setText(deal(v1));

        if (i * 2 + 1 < keys.size())
        {
            String k2 = keys.get(i * 2 + 1);
            String v2 = map.get(k2);

            viewHolder.name2.setText(k2);
            viewHolder.value2.setText(deal(v2));
        }







        return convertView;
    }

    private String deal(String v) {
        if (v == null)
        {
            return "";
        }
        else if (v.startsWith("."))
        {
            return "0"+v;
        }
        else if (v.equals("-1"))
        {
            return "关";
        }
        else if (v.equals("0"))
        {
            return "开";
        }
        else if (v.startsWith("-"))
        {
            return "";
        }
        else
        {
            return v;
        }
    }

    class ViewHolder {
        TextView name1;
        TextView value1;
        TextView name2;
        TextView value2;
    }

    class KV {
        String key;
        String value;

        public KV(String key, String value) {
            this.key = key;
            this.value = value;
        }
    }

    class KV2 {
        KV kv1;
        KV kv2;

        public KV getKv1() {
            return kv1;
        }

        public void setKv1(KV kv1) {
            this.kv1 = kv1;
        }

        public KV getKv2() {
            return kv2;
        }

        public void setKv2(KV kv2) {
            this.kv2 = kv2;
        }
    }
}
